<template>
  <paged-table
    ref="nftable"
    :entity-name="entityName"
    :request-param="pagerequest"
  >
    <template slot="FilterItems">
      <el-select
        v-model="pagerequest.state"
        class="filter-item "
      >
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
    </template>

    <template slot="TableColumns">
      <el-table-column
        :label="L('Actions')"
        width="100"
      >
        <template slot-scope="{row}">
          <el-link
            type="primary"
            style="margin-right:10px"
            @click="setNotificationAsRead(row.id)"
          >
            <svg-icon :icon-class="isReadIcon(row.state)" />
          </el-link>
          <el-link
            type="danger"
            icon="el-icon-delete"
            @click="deletNotification(row.id)"
          />
        </template>
      </el-table-column>

      <el-table-column
        prop="message"
        label="通知"
      >
        <template slot-scope="{row}">
          <i :class="row.notification.severity | notificationSeverityIcon" /> <span> {{ row.notification.data | formatMessage }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="creationTime"
        :label="L('CreationTime')"
      >
        <template slot-scope="{row}">
          <span>{{ row.notification.creationTime | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
    </template>

  </paged-table>
</template>

<script>
import pagedTable from '@/components/PagedTable'

export default {
  name: 'Notification',
  components: { pagedTable },
  data() {
    return {
      entityName: 'notification',
      pagerequest: { state: null },
      options: [{
        value: null,
        label: '全部'
      }, {
        value: 0,
        label: '未读'
      }, {
        value: 1,
        label: '已读'
      }]
    }
  },
  methods: {
    isReadIcon(state) {
      return state ? 'read' : 'unread'
    },

    async setNotificationAsRead(id) {
      await this.$store.dispatch('notification/setNotificationAsRead', id)
      this.$refs.nftable.getpage()
    },

    async setAllNotificationsAsRead() {
      await this.$store.dispatch('notification/setAllNotificationsAsRead')
      this.$refs.nftable.getpage()
    },

    deletNotification(id) {
      this.$confirm(`此通知将被删除`, '你确定吗删除？', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await this.$store.dispatch('notification/deleteNotification', id)
        this.$refs.nftable.getpage()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
